package com.todo.biz.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.todo.biz.application.convert.BizBusinessConvert;
import com.todo.biz.application.entity.bo.CommentBO;
import com.todo.biz.application.entity.condition.CommentConditionBO;
import com.todo.biz.application.handler.linstener.event.CounterEvent;
import com.todo.biz.application.handler.linstener.message.CounterMessage;
import com.todo.biz.application.service.BizExamineRecordService;
import com.todo.biz.application.service.CommentService;
import com.todo.biz.common.enums.CommentTypeEnum;
import com.todo.biz.common.enums.CounterTypeEnum;
import com.todo.biz.infrastructure.entity.condition.CommentCondition;
import com.todo.biz.infrastructure.entity.po.CommentPO;
import com.todo.biz.infrastructure.repository.CommentRepository;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:45
 */
@Slf4j
@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentRepository commentRepository;
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;
    @Resource
    private BizExamineRecordService bizExamineRecordService;

    @Override
    public Boolean insertComment(CommentBO commentBO) {
        if (commentBO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!commentBO.baseCheck()) {
            log.error("CommentService#insertComment 新增评论失败 基础字段校验未通过 commentBO：【{}】", JSONObject.toJSONString(commentBO));
            return false;
        }
        CommentPO commentPO = BizBusinessConvert.convertToCommentPO(commentBO);
        commentPO.setChildCount(0);
        commentPO.setLikeCount(0);
        String examineResult = bizExamineRecordService.localSensitiveWordsDetection(commentBO.getComments(), BusinessSceneEnum.BIZ_BASE);
        if (StringUtils.isEmpty(examineResult)){
            log.error("CommentService#insertComment 评论信息包含非法信息 comments:【{}】",commentPO.getComments());
            throw new BizException(BizErrorCodeEnum.COMMENTS_INFO_ILLEGAL);
        }
        commentPO.setComments(examineResult);
        Boolean insertResult = commentRepository.insertCommentPO(commentPO);
        if (insertResult && CommentTypeEnum.CONTENT.equals(commentBO.getCommentType())) {
            CounterMessage counterMessage = new CounterMessage();
            counterMessage.setBusinessScene(commentBO.getBusinessScene());
            counterMessage.setPlus(1);
            counterMessage.setCounterType(CounterTypeEnum.COMMENT);
            counterMessage.setBusinessId(commentBO.getParentId());
            counterMessage.setUserId(commentBO.getCommentUser());
            applicationEventPublisher.publishEvent(new CounterEvent(counterMessage));
        }
        return insertResult;
    }


    @Override
    public Boolean removeCommentById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CommentBO commentBO = queryCommentById(id);
        if (commentBO == null) {
            return true;
        }
        Boolean removeResult = commentRepository.removeCommentPOById(id);
        if (removeResult && CommentTypeEnum.CONTENT.equals(commentBO.getCommentType())) {
            CounterMessage counterMessage = new CounterMessage();
            counterMessage.setBusinessScene(commentBO.getBusinessScene());
            counterMessage.setPlus(-1);
            counterMessage.setCounterType(CounterTypeEnum.COMMENT);
            counterMessage.setBusinessId(commentBO.getParentId());
            counterMessage.setUserId(commentBO.getCommentUser());
            applicationEventPublisher.publishEvent(new CounterEvent(counterMessage));
        }
        return removeResult;
    }

    @Override
    public CommentBO queryCommentById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        CommentPO commentPO = commentRepository.queryCommentPOById(id);
        return BizBusinessConvert.convertToCommentBO(commentPO);
    }

    @Override
    public PageResult<CommentBO> queryCommentByCondition(CommentConditionBO conditionBO) {
        if (conditionBO == null) {
            return new PageResult<>();
        }
        CommentCondition commentCondition = BizBusinessConvert.convertToCommentCondition(conditionBO);
        PageResult<CommentPO> pageResult = commentRepository.queryCommentByCondition(commentCondition);
        if (pageResult == null) {
            return new PageResult<>();
        }
        PageResult<CommentBO> result = new PageResult<>();
        result.setResults(pageResult.getResults() == null ? null : pageResult.getResults().stream().map(BizBusinessConvert::convertToCommentBO).collect(Collectors.toList()));
        result.setHasNext(pageResult.isHasNext());
        result.setPageCount(pageResult.getPageCount());
        result.setPageNum(pageResult.getPageNum());
        result.setTotal(pageResult.getTotal());
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setPageSize(pageResult.getPageSize());
        return result;
    }

}
